<?php

class Zend_View_Helper_UrlMaker extends Zend_View_Helper_Abstract
{
	protected $_url;
	 /**
 	  * make archor setting controller, action, param
 	  * has 4 params, but when setting 3 param it 
 	  * look at third one as a message 
 	  * 
 	  * @param string $controller 
 	  * @param string $action 
 	  * @param array $params
 	  * @param string $message  
 	  * @return string (str of  archor) 
 	  */
	
	public function urlMaker($controller, $action = 'index', $params = array(), $message = '' ){

		$this->_url =  new Zend_View_Helper_Url();
		$url = $this->_url->url(array('controller' => $controller, 'action' => $action));
		$urlQuery = $this->_setUrlQuery($params);
		if(!empty($urlQuery)){
			$url .= $urlQuery; 
		}else{
			$message = $urlQuery;
		}
		$archor = '<a href='.$url.'>'.$message.'</a>';
			
		return $archor;
			
	}
	/**
	 * 
	 * set query of a url
	 * if @param $param is not an array
	 * return it and then it will initialise an message   
	 * @param  $params
	 * @return string $urlQuery (otherwise false)
	 */
	private function _setUrlQuery($params){
		
		$urlQuery = '';
		
		if(!empty($params)){
			$urlQuery .= '?';
			if(is_array($params))
			{
				$i = 0;
				$paramNumber = count($params);
				foreach ($params as $param => $val)
				{
					$urlQuery .= $param.'='.urlencode($val);
					if(++$i < $paramNumber){
						$urlQuery .= '&';
					}
				}
				
				return $urlQuery;
			}else{
				//if it is not array but it exists, return $params
			    // for message initialisation instead param
				return $params;
			}
		}else{
			return false;
		}
	}
}